package cn.fancylab.template.xmpp.controll;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.eboo.openfire.cmd.ST_CMD;
import com.eboo.openfire.vo.UploadData;

import cn.fancylab.model.bs.DeviceCabinetBox;
import cn.fancylab.model.bs.DeviceController;
import cn.fancylab.model.bs.DeviceGateway;
import cn.fancylab.template.service.BaseAppService;
import cn.fancylab.template.util.ByteUtil;
import cn.fancylab.template.util.JsonUtil;
import cn.fancylab.untrans.impl.AbstractCacheService;
import cn.fancylab.xmpp.magent.SmackAgentClient;
import cn.fancylab.xmpp.magent.WebContainerMonitorAgent;

/**
 * openfire消息监听注册类
 * <p>
 * 用于各个中控通过openfire上传信息使用
 * </p>
 * 
 * @author Administrator
 */
public class DeviceStateInfoMonitorAgent implements InitializingBean {

	@Autowired
	private WebContainerMonitorAgent monitorAgent;

	@Autowired
	@Qualifier("baseAppService")
	private BaseAppService baseAppService;

	@Autowired
	@Qualifier("redisCacheService")
	private AbstractCacheService redisCacheService;

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("DeviceStateInfoMonitorAgent");
		SmackAgentClient client = monitorAgent.getClient();
		client.registerFixedCallback((message) -> {
			String subject = message.getSubject();
			// 注册监听事件，接收消息
			if (StringUtils.equals(subject, "device_state")) {
				UploadData uploadData = JsonUtil.deserialize(message.getBody(), UploadData.class);

				DeviceController deviceController = null;
				DeviceGateway deviceGateway = null;
				DeviceCabinetBox deviceCabinetBox = null;
				Map<String, Object> map;

				if (StringUtils.isNotBlank(uploadData.getControllerIdentifiedNo())) {
					map = new HashMap<String, Object>();
					map.put("identified_no", uploadData.getControllerIdentifiedNo());
					map.put("is_del", 0);
					deviceController = baseAppService.getObjectByProperty(DeviceController.class, map);
				}

				if (StringUtils.isNotBlank(uploadData.getGatewayIdentifiedNo()) && null != deviceController) {
					map = new HashMap<String, Object>();
					map.put("bs_d_controller_uuid", deviceController.getUuid());
					map.put("identified_no", uploadData.getGatewayIdentifiedNo());
					map.put("is_del", 0);
					deviceGateway = baseAppService.getObjectByProperty(DeviceGateway.class, map);
				}

				if (uploadData.getAddr() > 0 && deviceGateway != null) {
					map = new HashMap<String, Object>();
					map.put("bs_d_gateway_uuid", deviceGateway.getUuid());
					map.put("address_485", ByteUtil.toHex(uploadData.getAddr()));
					map.put("is_del", 0);
					deviceCabinetBox = baseAppService.getObjectByProperty(DeviceCabinetBox.class, map);
				}

				switch (uploadData.getCmdType()) {
				case ST_CMD.MSG_WEIGHT_RESET_ZERO: // 去皮重

					break;
				case ST_CMD.MSG_WEIGHT_CALIBRATION: // 标定
					break;
				case ST_CMD.MSG_SEARCH: // 设备搜索

					break;
				case ST_CMD.MSG_SET_ALIAS: // 设置别名
					break;
				case ST_CMD.MSG_GET_ALIAS: // 获取别名
					if (null != deviceCabinetBox) {
						deviceCabinetBox.setAlias_name(uploadData.getCmdResultData());
					}
					break;
				case ST_CMD.MSG_SET_TARGET_SIG_WEIGHT: // 设置称重目标的单个重量
					break;
				case ST_CMD.MSG_GET_WEIGHT: // 获取当前的重量
					if (null != deviceCabinetBox) {
						deviceCabinetBox.setWeight_value(uploadData.getCmdResultData());
					}
					String key = deviceController.getIdentified_no().concat("_")
							.concat(deviceGateway.getIdentified_no()).concat("_")
							.concat(deviceCabinetBox.getAddress_485());
					redisCacheService.set(redisCacheService.REGION_TWENTYMIN, key, uploadData.getCmdResultData());

					String uploadDatatemp = (String) redisCacheService.get(redisCacheService.REGION_TWENTYMIN, key);
					System.out.println(uploadDatatemp);
					break;
				case ST_CMD.MSG_GET_COUNT: // 获取当前的个数
					if (null != deviceCabinetBox) {
						deviceCabinetBox.setNum_value(uploadData.getCmdResultData());
					}
					break;
				case ST_CMD.MSG_SET_TARGET_SIG_WEIGHT_ERR: // 设置单重误差
					break;
				case ST_CMD.MSG_LOCKER_POWER: // 锁状态

					break;
				case ST_CMD.MSG_LED_STATE: // LED状态

					break;
				case ST_CMD.MSG_GET_GATEWAY_CODE: // 获取网关板编号
					if (null != deviceGateway) {
						deviceGateway.setIdentified_no(uploadData.getCmdResultData());
					}
					break;
				default:
					break;
				}
			}
			return null;
		});
	}
}
